package com.tbq.tbqj_ggl.activity

import android.view.View
import com.tbq.sdk.L
import com.tbq.sdk.TBQ
import com.tbq.tbqj_ggl.R
import com.tbq.tbqj_ggl.model.Prizes
import com.tbq.tbqj_ggl.utils.AnimationUtils
import com.tbq.tbqj_ggl.utils.RandomUtil
import com.tbq.tbqj_ggl.view.ScratchImageView
import com.tbq.tbqj_ggl.view.explosionfield.ExplosionField
import pw.hais.http.Http


/**
 * 一卦千金
 * Created by hais1992 on 2017/5/11.
 */

class ScratchFKLCGameActivity : ScrachGameBaseActivity(if (TBQ.get("KEY_IS_MOBILE") == "1") R.layout.mobile_scratch_fklc_game else
    R.layout.activity_scratch_fklc_game), ScratchImageView.OnScratchListent, AnimationUtils.TranslateListenter, View.OnClickListener {
    override fun onClick(p0: View?) {
        if (scratch_jp_1.isScratch || scratch_jp_2.isScratch || scratch_jp_3.isScratch) {
            scratch_jp_1.setComplete(true)
            scratch_jp_2.setComplete(true)
            scratch_jp_3.setComplete(true)
            scratch_ls_1.setComplete(true)
            scratch_ls_2.setComplete(true)
            scratch_ls_3.setComplete(true)
        } else {
            L.showShort("要先刮动奖品涂层才可以直接 兑奖 哟！")
        }
    }

    override fun onScratchItem(view: ScratchImageView?) {
        findViewById(R.id.text_start).setBackgroundDrawable(resources.getDrawable(R.drawable.scratch_game_btzlyc))
    }

    //缆车
    val layout_lc_1 by lazy { findViewById(R.id.layout_lc_1) }
    val layout_lc_2 by lazy { findViewById(R.id.layout_lc_2) }
    val layout_lc_3 by lazy { findViewById(R.id.layout_lc_3) }
    //缆绳
    val image_ls_1 by lazy { findViewById(R.id.image_ls_1) }
    val image_ls_2 by lazy { findViewById(R.id.image_ls_2) }
    val image_ls_3 by lazy { findViewById(R.id.image_ls_3) }
    //障碍，卡带
    val image_kd_1 by lazy { findViewById(R.id.image_kd_1) }
    val image_kd_2 by lazy { findViewById(R.id.image_kd_2) }
    val image_kd_3 by lazy { findViewById(R.id.image_kd_3) }
    //涂刮层
    val scratch_ls_1 by lazy { findViewById(R.id.scratch_ls_1) as ScratchImageView }
    val scratch_ls_2 by lazy { findViewById(R.id.scratch_ls_2) as ScratchImageView }
    val scratch_ls_3 by lazy { findViewById(R.id.scratch_ls_3) as ScratchImageView }
    //奖品
    val scratch_jp_1 by lazy { findViewById(R.id.scratch_jp_1) as ScratchImageView }
    val scratch_jp_2 by lazy { findViewById(R.id.scratch_jp_2) as ScratchImageView }
    val scratch_jp_3 by lazy { findViewById(R.id.scratch_jp_3) as ScratchImageView }

    val explosionField by lazy { ExplosionField.attach2Window(this) }
    var winIndex = 0
    var defultScaleX = 0f
    var defultScaleY1 = 0f
    var defultScaleY2 = 0f
    var defultScaleY3 = 0f
    var defultAlpha = 0f

    private var isWin: Boolean = false


    override fun initDataAndView() {
        scratch_ls_1.setOnScratchListent(this)
        scratch_ls_2.setOnScratchListent(this)
        scratch_ls_3.setOnScratchListent(this)

        scratch_jp_1.setOnScratchListent(this)
        scratch_jp_2.setOnScratchListent(this)
        scratch_jp_3.setOnScratchListent(this)

        defultScaleX = layout_lc_1.scaleX
        defultScaleY1 = layout_lc_1.scaleY
        defultScaleY2 = layout_lc_2.scaleY
        defultScaleY3 = layout_lc_3.scaleY
        defultAlpha = layout_lc_1.alpha

        findViewById(R.id.text_start).setOnClickListener(this)
    }

    override fun initDataToStartGame(challengePrize: Prizes, isWin: Boolean) {
        super.initDataToStartGame(challengePrize, isWin)
        this.isWin = isWin
        fullNumber = 0
        scratch_ls_1.resteData()
        scratch_ls_2.resteData()
        scratch_ls_3.resteData()

        scratch_jp_1.resteData()
        scratch_jp_2.resteData()
        scratch_jp_3.resteData()

        layout_lc_1.alpha = defultAlpha
        layout_lc_2.alpha = defultAlpha
        layout_lc_3.alpha = defultAlpha
        layout_lc_1.scaleX = defultScaleX
        layout_lc_2.scaleX = defultScaleX
        layout_lc_3.scaleX = defultScaleX
        layout_lc_1.scaleY = defultScaleY1
        layout_lc_2.scaleY = defultScaleY2
        layout_lc_3.scaleY = defultScaleY3
        layout_lc_1.x = 10f
        layout_lc_2.x = 10f
        layout_lc_3.x = 10f
        layout_lc_1.alpha = 1f
        layout_lc_1.y = image_kd_1.y + 10
        layout_lc_2.y = image_kd_2.y + 10
        layout_lc_3.y = image_kd_3.y + 10


        //随机位置 拦截
        val kd1 = RandomUtil.getRandom(image_kd_1.width + 100, image_ls_1.width - 300 - image_kd_1.width)
        val kd2 = RandomUtil.getRandom(image_kd_2.width + 100, image_ls_2.width - 300 - image_kd_2.width)
        val kd3 = RandomUtil.getRandom(image_kd_2.width + 100, image_ls_3.width - 300 - image_kd_3.width)
        image_kd_1.x = kd1 * 1f
        image_kd_2.x = kd2 * 1f
        image_kd_3.x = kd3 * 1f
        image_kd_1.visibility = View.VISIBLE
        image_kd_2.visibility = View.VISIBLE
        image_kd_3.visibility = View.VISIBLE

        val winIndex = if (isWin) RandomUtil.getRandom(1, 3) else 0
        when (winIndex) {
            1 -> image_kd_1.visibility = View.GONE
            2 -> image_kd_2.visibility = View.GONE
            3 -> image_kd_3.visibility = View.GONE
        }

        //加载奖品
        Http.displayImageAutoSize(scratch_jp_1, SERVER_URL + challengePrize.Icon)
        Http.displayImageAutoSize(scratch_jp_2, SERVER_URL + challengePrize.Icon)
        Http.displayImageAutoSize(scratch_jp_3, SERVER_URL + challengePrize.Icon)
    }

    override fun onScratchFull(view: ScratchImageView?) {
        fullNumber++
        when (view?.id) {
            R.id.scratch_ls_1 -> {
                if (image_kd_1.visibility != View.VISIBLE) image_kd_1.x = (image_ls_1.width - 100 - image_kd_1.width) * 1f
                AnimationUtils.translateSetX(layout_lc_1, layout_lc_1.x, image_kd_1.x, layout_lc_1.translationY, layout_lc_1.translationY, 0f, 2000, true, this)
            }
            R.id.scratch_ls_2 -> {
                if (image_kd_2.visibility != View.VISIBLE) image_kd_2.x = (image_ls_2.width - 100 - image_kd_2.width) * 1f
                AnimationUtils.translateSetX(layout_lc_2, layout_lc_2.x, image_kd_2.x, layout_lc_2.translationY, layout_lc_2.translationY, 0f, 2000, true, this)

            }
            R.id.scratch_ls_3 -> {
                if (image_kd_3.visibility != View.VISIBLE) image_kd_3.x = (image_ls_3.width - 100 - image_kd_3.width) * 1f
                AnimationUtils.translateSetX(layout_lc_3, layout_lc_3.x, image_kd_3.x, layout_lc_3.translationY, layout_lc_3.translationY, 0f, 2000, true, this)
            }
            else -> onAnimationEnd(view!!)
        }
    }

    private var fullNumber = 0
    override fun onAnimationEnd(v: View) {
        if (!isPlayGameing) {
            L.showShort("请先开始游戏！")
            return
        }

        when (v.id) {
            R.id.layout_lc_1 -> {
                if (winIndex != 1) {
                    scratch_jp_1.setComplete(true)
                    v.postDelayed({ explosionField.explode(v) }, 1000)
                }
            }
            R.id.layout_lc_2 -> {
                if (winIndex != 2) {
                    scratch_jp_2.setComplete(true)
                    v.postDelayed({ explosionField.explode(v) }, 1000)
                }
            }
            R.id.layout_lc_3 -> {
                if (winIndex != 3) {
                    scratch_jp_3.setComplete(true)
                    v.postDelayed({ explosionField.explode(v) }, 1000)
                }
            }
        }

        if (fullNumber == 6) {
            findViewById(R.id.text_start).setBackgroundDrawable(resources.getDrawable(R.drawable.scratch_game_btzlyc_hs))
            v.postDelayed({ showGameResultWindows(isWin) }, 2000)
        }
    }

}